package cn.hx.mall.order.service.impl;

import cn.hx.mall.order.dao.OrderReturnReasonDao;
import cn.hx.mall.order.entity.OrderReturnReason;
import cn.hx.mall.order.service.OrderReturnReasonService;
import cn.hx.mall.permissions.entity.Role;
import cn.hx.mall.permissions.entity.page.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 退货原因表(OrderReturnReason)表服务实现类
 *
 * @author makejava
 * @since 2022-11-02 17:57:09
 */
@Service("orderReturnReasonService")
public class OrderReturnReasonServiceImpl implements OrderReturnReasonService {
    @Resource
    private OrderReturnReasonDao orderReturnReasonDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OrderReturnReason queryById(Long id) {
        return this.orderReturnReasonDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<OrderReturnReason> queryAllByLimit(OrderReturnReason orderReturnReason, int offset, int limit) {
        return orderReturnReasonDao.queryAllByLimit(orderReturnReason,offset,limit);
    }

    /**
     * 新增数据
     *
     * @param orderReturnReason 实例对象
     * @return 实例对象
     */
    @Override
    public OrderReturnReason insert(OrderReturnReason orderReturnReason) {
        this.orderReturnReasonDao.insert(orderReturnReason);
        return orderReturnReason;
    }

    /**
     * 修改数据
     *
     * @param orderReturnReason 实例对象
     * @return 实例对象
     */
    @Override
    public OrderReturnReason update(OrderReturnReason orderReturnReason) {
        this.orderReturnReasonDao.update(orderReturnReason);
        return this.queryById(orderReturnReason.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.orderReturnReasonDao.deleteById(id) > 0;
    }

    @Override
    public List<OrderReturnReason> queryAll(OrderReturnReason orderReturnReason) {
        return orderReturnReasonDao.queryAll(orderReturnReason);
    }

    @Override
    public int count() {
        return orderReturnReasonDao.count();
    }

    @Override
    public Page getPageInfo(Page page) {
        if(page.getPage()==null){
            page.setPage(1);
        }if(page.getLimit()==null){
            page.setLimit(10);
        }
        OrderReturnReason orderReturnReason = new OrderReturnReason();
        orderReturnReason.setName(page.getSearch());
        int count = orderReturnReasonDao.count();
        int size = page.getLimit();
        int cntPage = count%size==0?count/size:(count/size+1);
        page.setCntpage(cntPage);
        page.setCount(count);
        return page;
    }


}